package com.jin.jpa.wrapper.expression;

import com.jin.jpa.wrapper.LambdaWrapperContext;
import org.hibernate.query.criteria.internal.OrderImpl;

import javax.persistence.criteria.Order;

/**
 * @Author: yuanxiuyao
 * @Date: 2024/1/4 15:07
 * @PackageName: org.example.jpa.wrapper
 * @Version 1.0
 */
public class OrderByExpression implements IExpression<Order>{

    private String expression;
    private boolean ascending;

    public OrderByExpression(String expression, boolean ascending){
        this.expression = expression;
        this.ascending = ascending;
    }

    public static OrderByExpression asc(String expression){
        return new OrderByExpression(expression, true);
    }

    public static OrderByExpression desc(String expression){
        return new OrderByExpression(expression, false);
    }

    @Override
    public Order toHandle(LambdaWrapperContext<?> context) {
        return new OrderImpl(context.getRoot().get(expression), ascending);
    }
}
